package com.lanou.myapplication.PhotoRecyclerAdapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lanou.myapplication.R;
import com.lanou.myapplication.entity.Phototwo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/26.
 */
public class SecondphotoRecyclerAdapter extends
        RecyclerView.Adapter<SecondphotoRecyclerAdapter.SecondphotoViewHolder>{
    private List<Phototwo.DataEntity.ListEntity> data; //存放data类型的集合
    private RequestQueue queue; //定义一个Volley的请求队列

    //初始化的方法 一般在构造方法中进行
    public SecondphotoRecyclerAdapter(Context c) {
        queue = Volley.newRequestQueue(c);// 初始化请求队列

    };
    public void addData(List<Phototwo.DataEntity.ListEntity> list) {
        data  = list;
        notifyDataSetChanged();
    }



    class SecondphotoViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView title;

        private  SecondphotoViewHolder(View v){
            super(v);
            imageView=(ImageView)v.findViewById(R.id.photo_img2);
            title=(TextView)v.findViewById(R.id.photo_title2);
        }

    }

    @Override
    public int getItemCount() {
        return data != null && data.size() > 0 ? data .size(): 0;
    }

    public void onBindViewHolder(SecondphotoViewHolder holder, int position) {
        if (data != null && data.size()> 0) {
            Phototwo.DataEntity.ListEntity p =data.get(position);
            holder.title.setText(p.getTitle());
            if (p.getPic() != null && p.getPic().length() > 0) {
                String pic = p.getPic();


                getBitmap(pic,holder.imageView);
            }


        }
    }



    private void getBitmap(String img,final ImageView imageView ) {

        ImageRequest imageRequest = new ImageRequest(img,
                new Response.Listener<Bitmap>() {

                    @Override
                    public void onResponse(Bitmap response) {
                        imageView.setImageBitmap(response);

                    }
                }, 300, 300, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();

            }
        }){@Override
           public Map<String, String> getHeaders()
                throws AuthFailureError {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("Accept-Encoding", "");//添加方法头  避免出现缓存问题
            return map;


        }};
        queue.add(imageRequest);

    }



    @Override
    public SecondphotoViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View view = inflater.inflate(R.layout.item_photo2, null);
        return new SecondphotoViewHolder(view) ;
    }
}

